// pages/changcity/changcity.js
//pages/seltelcommunity/seltelcommunity.js
//获取应用实例
var Ajax = require('../../utils/Ajax.js')
var utils = require('../../utils/util.js');
var config = require('../../base.conf.js').config;
const app = getApp();
var ctimeout = null;
Page({
  data: {
    imgurl: app.getApi('download'),
    queryname: '',
    searchArr: [],
    resultsArr: [],
    tapinput: false,
  },
  onLoad: function(option) {
    option.city = option.city ? option.city : "上海";
    this.setData({
      model: app.globalData.model,
      option
    })
    const sysInfo = wx.getSystemInfoSync();
    const screenWidth = sysInfo.screenWidth;
    this.factor = screenWidth / 750;
    this.getCityHavingQuarters();
  },
  onReady() {
    var query = wx.createSelectorQuery();
    query.select('.pages').boundingClientRect((rect) => {
      this.setData({
        nogoodsH: (rect.height / this.factor) - 68 - 60,
      })
    }).exec();
  },
  delText(e) { //清空input
    this.setData({
      ['' + e.detail]: '',
      tapinput: false,
    })
  },
  changCity(e) {
    var {
      item
    } = e.currentTarget.dataset;
    var pages = getCurrentPages();
    var currentPage = pages[pages.length - 2];
    currentPage.setData({
      nearObj: item
    }, () => {
      wx.navigateBack()
    })
  },
  tapinput() {
    var {
      resultsArr,
      searchArr
    } = this.data;
    this.setData({
      tapinput: true,
      searchArr: searchArr.length > 0 ? searchArr : resultsArr,
    })
  },
  bindinputval(e) { //问题描述输入的字符控制
    var {
      name
    } = e.currentTarget.dataset;
    var {
      resultsArr
    } = this.data;
    var val = e.detail.value;
    val = val.replace(/\s+/g, "");
    this.setData({
      [name]: val
    }, () => {
      var arr = [] // 临时数组 用于存放匹配的数组
      for (let i in resultsArr) {
        if (resultsArr[i].name.indexOf(val) >= 0) {
          arr.push(resultsArr[i])
        }
      }
      if (arr.length == 0) {
        this.setData({
          searchArr: resultsArr,
        })
      } else {
        this.setData({
          searchArr: arr,
        })
      }
    })
  },
  getCityHavingQuarters() {
    var {
      queryname
    } = this.data;
    Ajax.get('getCityHavingQuarters', {
      cityName: queryname
    }).then((d) => {
      if (d.status == 200 && d.data) {
        var list = d.data || [];
        this.setData({
          resultsArr: d.data || [],
          statusType: list.length > 0 ? 'none' : 'empty',
        })
      } else {
        var {
          resultsArr
        } = this.data;
        this.setData({
          statuserro: resultsArr.length > 0 ? false : true,
          statusType: resultsArr.length > 0 ? 'none' : 'empty',
        })
        utils.showtoast(d.msg || '服务器错误,查询失败')
      }
    }).catch((e) => {
      console.log(e)
      var {
        resultsArr
      } = this.data;
      this.setData({
        statuserro: resultsArr.length > 0 ? false : true,
        statusType: resultsArr.length > 0 ? 'none' : 'empty',
      })
      utils.showtoast('网络错误查询失败')
    })
  },
  onShow() {

  },
  refresh() { //刷新
    this.getCityHavingQuarters();
  },
  onReachBottom: function() {},
  onPullDownRefresh: function() {
    this.refresh()
  },
  //没有UID弹窗
  shownoUidModal(id) {
    var modal = this.selectComponent("#" + id || 'noUid');
    modal.show();
  },
  closeModal(e) { //关闭弹窗
    var id = this.onSetid(e);
    var modal = this.selectComponent("#" + (id || 'modal'));
    modal.hide();
  },
  onSetid(e) { //弹出窗的ID获取
    var id = 'modal'
    if (typeof(e) == 'string') {
      id = e
    } else {
      id = e && (e.currentTarget.id || e.currentTarget.dataset.id);
    }
    return id;
  },
  onShareAppMessage(e) {
    return {
      title: '邻里邻外',
      path: `/pages/index/index`,
    }
  }
})